package core.util;

import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

public class BaseService<D extends BaseDao<T>,T extends BaseEntity<T>> {
	@Autowired
	private D dao;
	
	public D getDao() {
		return dao;
	}
	
	public int insert(T obj) {
		obj.setId(WebUtil.UUID());
		obj.setAddtime(new Date());
		obj.setEdittime(obj.getAddtime());
		obj.setDelflag("0");
		return dao.insert(obj);
	}

	public int update(T obj) {
		obj.setEdittime(new Date());
		if (WebUtil.isBlank(obj.getDelflag())) {
			obj.setDelflag("0");
		}
		return dao.update(obj);
	}
	
	public int save(T obj) {
		if (WebUtil.isBlank(obj.getId())) {
			return insert(obj);
		}else {
			return update(obj);
		}
	}

	public int delete(String id) {
		return dao.delete(id);
	}

	public T get(String id) {
		return dao.get(id);
	}

	public List<T> findList(T condition) {
		return dao.findList(condition);
	}

	@Transactional(readOnly = true)
	public Page<T> findPage(T condition) {
		condition.useAutoPaging();
		List<T> list = dao.findList(condition);
		return condition.getPage().returnPage(list);
	}
}
